package o;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RecentTaskInfo;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build.VERSION;
import android.view.KeyEvent;
import java.util.List;

public abstract class djc {
    public static boolean ˊ(int i, KeyEvent keyEvent, Activity activity) {
        if (4 != i || activity == null || activity.getIntent() == null) {
            return false;
        }
        boolean booleanExtra;
        boolean booleanExtra2;
        try {
            booleanExtra = activity.getIntent().getBooleanExtra("activity_open_from_notification_flag", false);
        } catch (Throwable e) {
            Logger.ˋ("ActivityBackUtil", "error!", e);
            booleanExtra = false;
        }
        try {
            booleanExtra2 = activity.getIntent().getBooleanExtra("activity_back_to_market_activity_flag", false);
        } catch (Throwable e2) {
            Logger.ˋ("ActivityBackUtil", "error!", e2);
            booleanExtra2 = false;
        }
        if (!booleanExtra) {
            return false;
        }
        Logger.ˎ("BaseActivity", "isFromNotification :" + booleanExtra);
        activity.finish();
        ˎ(activity, booleanExtra2);
        return true;
    }

    private static void ˎ(Activity activity, boolean z) {
        PackageManager packageManager = ContentManager.getInstant().getContext().getPackageManager();
        ActivityManager activityManager = (ActivityManager) ContentManager.getInstant().getContext().getSystemService("activity");
        djj ˋ = ˋ(packageManager, activityManager);
        if (ˋ == null) {
            Logger.ˎ("ActivityBackUtil", "TaskDescription td is null");
            if (z) {
                cjk.ˎ().ˋ(activity, new cjm("market.activity", null));
                return;
            }
            Intent launchIntentForPackage = packageManager.getLaunchIntentForPackage(ContentManager.getInstant().getContext().getPackageName());
            if (launchIntentForPackage != null) {
                launchIntentForPackage.addCategory("android.intent.category.LAUNCHER");
            }
            if (launchIntentForPackage != null) {
                ContentManager.getInstant().getContext().startActivity(launchIntentForPackage);
                return;
            }
            return;
        }
        ˎ(activityManager, ˋ);
    }

    private static void ˎ(ActivityManager activityManager, djj o_djj) {
        if (o_djj.ˎ >= 0) {
            Logger.ˎ("ActivityBackUtil", "moveTaskToFront taskId:" + o_djj.ˎ);
            activityManager.moveTaskToFront(o_djj.ˎ, 1);
            return;
        }
        Logger.ˎ("ActivityBackUtil", "taskId is 0,so startActivity");
        Intent intent = o_djj.ॱ;
        intent.addFlags(269500416);
        try {
            ContentManager.getInstant().getContext().startActivity(intent);
        } catch (Throwable e) {
            Logger.ˋ("ActivityBackUtil", "startActivity error!!", e);
        } catch (Throwable e2) {
            Logger.ˋ("ActivityBackUtil", "startActivity error!!", e2);
        }
    }

    private static djj ˏ(int i, int i2, Intent intent, ComponentName componentName) {
        Intent intent2 = new Intent(intent);
        if (componentName != null) {
            intent2.setComponent(componentName);
        }
        PackageManager packageManager = ContentManager.getInstant().getContext().getPackageManager();
        intent2.setFlags((intent2.getFlags() & -2097153) | 268435456);
        ResolveInfo resolveActivity = packageManager.resolveActivity(intent2, 0);
        if (resolveActivity != null) {
            ActivityInfo activityInfo = resolveActivity.activityInfo;
            if (activityInfo.packageName.equals(ContentManager.getInstant().getContext().getPackageName()) && dgm.ˎ() == i2) {
                return new djj(i, i2, resolveActivity, intent, activityInfo.packageName);
            }
        }
        return null;
    }

    private static boolean ˏ(PackageManager packageManager, ComponentName componentName, ActivityInfo activityInfo) {
        if (activityInfo == null) {
            activityInfo = new Intent("android.intent.action.MAIN").addCategory("android.intent.category.HOME").resolveActivityInfo(packageManager, 0);
        }
        if (activityInfo != null && activityInfo.packageName.equals(componentName.getPackageName()) && activityInfo.name.equals(componentName.getClassName())) {
            return true;
        }
        return false;
    }

    @SuppressLint({"NewApi"})
    private static djj ˋ(PackageManager packageManager, ActivityManager activityManager) {
        List recentTasks = activityManager.getRecentTasks(21, 2);
        if (recentTasks == null) {
            return null;
        }
        int size = recentTasks.size();
        ActivityInfo resolveActivityInfo = new Intent("android.intent.action.MAIN").addCategory("android.intent.category.HOME").resolveActivityInfo(packageManager, 0);
        for (int i = 0; i < size; i++) {
            RecentTaskInfo recentTaskInfo = (RecentTaskInfo) recentTasks.get(i);
            Intent intent = new Intent(recentTaskInfo.baseIntent);
            if (recentTaskInfo.origActivity != null) {
                intent.setComponent(recentTaskInfo.origActivity);
            }
            if (!ˏ(packageManager, intent.getComponent(), resolveActivityInfo)) {
                if (VERSION.SDK_INT <= 11) {
                    return null;
                }
                djj ˏ = ˏ(recentTaskInfo.id, recentTaskInfo.persistentId, recentTaskInfo.baseIntent, recentTaskInfo.origActivity);
                if (ˏ != null) {
                    return ˏ;
                }
            }
        }
        return null;
    }

    public static int ˋ() {
        long ˋ = dok.ˋ(ContentManager.getInstant().getContext());
        if (ˋ <= 536870912) {
            return 6;
        }
        if (ˋ <= 1073741824) {
            return 10;
        }
        if (ˋ <= 1610612736) {
            return 12;
        }
        if (ˋ <= 2147483648L) {
            return 15;
        }
        return ˋ <= 4294967296L ? 18 : 18;
    }
}
